/**
 * @(#)ApkFilterController.java 2016年12月9日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.annotation.SystemControllerLog;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.dao.IBaseInfoDAO;
import org.jsbd.boss.domian.channel.ApkFilter;
import org.jsbd.boss.domian.channel.BrushApkConfig;
import org.jsbd.boss.domian.channel.BrushConfig;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.service.IApkFilterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户数据过滤
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/apkFilter")
public class ApkFilterController {
	public static final Logger LOGGER = LoggerFactory.getLogger(ApkFilterController.class);
	@Resource
	private IApkFilterService apkFilterService;
	
	@Resource
	private IBaseInfoDAO baseInfoDAO;
	

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		return "apkFilter/list";
	}

	@RequestMapping(value = "list", params = "json")
	@ResponseBody
	public Map<String, Object>  queryList(DataGridModel page, ApkFilter apkFilter) throws Exception {
		Map<String, Object> map = new HashMap<>();
		String apkName = apkFilter.getApkName();
		if(StringUtils.isNotBlank(apkName)){
			String[] keys = apkName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : keys) {
				keywords.add(keyword);
			}
			apkFilter.setKeywords(keywords);
		}

		Integer filterType = apkFilter.getFilterType();
		if(filterType != null && filterType == -1){
			apkFilter.setFilterType(null);
		}
		map.put("rows", apkFilterService.selectAllList(apkFilter));
		map.put("total", apkFilterService.selectCount(apkFilter));
		return map;
	}



	@RequestMapping(value = "{id}/update", method = RequestMethod.GET)
	public String update(ApkFilter apkFilter, Model model) {
		model.addAttribute("apkFilter", apkFilterService.selectApkFilter(apkFilter));
		return "apkFilter/update";
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.POST)
	public void update(HttpServletResponse response, ApkFilter apkFilter) {
		try {
			String baseName = baseInfoDAO.getBaseInfoById(apkFilter.getBaseId()).getBaseName();
			apkFilter.setBaseName(baseName);
			apkFilterService.updateApkFilter(apkFilter);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "{id}/delete", method = RequestMethod.POST)
	public void del(HttpServletResponse response, @PathVariable Integer id) {
		try {
			apkFilterService.deleteApkFilter(id);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "apkFilter/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public void add(HttpServletResponse response, ApkFilter apkFilter) {
		try {
			String baseName = baseInfoDAO.getBaseInfoById(apkFilter.getBaseId()).getBaseName();
			apkFilter.setBaseName(baseName);
			apkFilterService.insertApkFilter(apkFilter);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}


	@RequestMapping(value = "{id}/apkList")
	public String apkList(ApkFilter apkFilter, Model model) {
		model.addAttribute("filterId", apkFilter.getId());
		return "apkFilter/addApkList";
	}


	@RequestMapping(value = "{filterId}/apkListConfig", params = "json")
	@ResponseBody
	public Map<String, List<ApkFilter>> apkListConfig(ApkFilter apkFilter, Model model) {
		Map<String, List<ApkFilter>> map = new HashMap<String, List<ApkFilter>>();
		String apkName = apkFilter.getApkName();
		if(StringUtils.isNotBlank(apkName)){
			String[] keys = apkName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : keys) {
				keywords.add(keyword);
			}
			apkFilter.setKeywords(keywords);
		}
		map.put("rows", apkFilterService.selectApkList(apkFilter));
		return map;
	}


	@RequestMapping(value = "saveApk", method = RequestMethod.POST)
	public void saveApk(ApkFilter apkFilter, HttpServletResponse response) {
		apkFilterService.insertApk(apkFilter);
		ResponseUtils.responseSuccess(response);
	}


	@RequestMapping(value = "delApk", method = RequestMethod.POST)
	public void delApk(ApkFilter apkFilter, HttpServletResponse response) {
		apkFilterService.deleteApk(apkFilter);
		ResponseUtils.responseSuccess(response);
	}
}
